/*
 * Ethan Levine
 * 
 * The contents of this file are subject to the GNU General Public
 * License (GPL) v2.  The full contents of this license can be
 * found at Mathma's website, http://www.codeplex.com/mathma/license
 */

package mathma.core.real.form;

import java.util.List;
import java.util.ArrayList;

import mathma.core.form.MultiExpression;
import mathma.core.form.UnknownSuspensionException;

import mathma.core.expr.Variable;
import mathma.core.expr.Expression;
import mathma.core.real.Real;

/**
 * @since 0.1.5
 * @author Ethan Levine
 */
public class AdditionExpression extends MultiExpression<Real, Real> {

    public AdditionExpression(Expression<Real>... operands) {
        super(Real.Type, operands);
    }
    
    public AdditionExpression(List<Expression<Real>> operands) {
        super(Real.Type, operands);
    }
    
    public Real evaluate() {
        Real total = Real.ZERO;
        for (Expression<Real> operand : getOperands()) {
            total.add(operand.evaluate());
        }
        return total;
    }

    @Override
    public Expression<Real> differentiate(Variable<Real> var)
            throws UnknownSuspensionException {
        List<Expression<Real>> newOperands = new ArrayList<Expression<Real>>();
        for (Expression<Real> operand : getOperands()) {
            newOperands.add(operand.differentiate(var));
        }
        return new AdditionExpression(newOperands);
    }
    
    public AdditionExpression newInstance(List<Expression<Real>> operands) {
        return new AdditionExpression(operands);
    }
}
